﻿using System;
using System.Web.UI.WebControls;
//
using PerceptiveMCAPI.Types;
//
namespace PerceptiveMCAPI_Test
{
   public partial class show_apikeys : System.Web.UI.UserControl
   {
      public void Display( apikeysOutput output )
      {
         this.Visible = true;
         hidCount.Value = output.result.Count.ToString();
         //
         gvApikeys.DataSource = output.result;
         if ( output.inputParms.expired == false )
         {
            gvApikeys.Columns[0].Visible = true;
            gvApikeys.Columns[3].Visible = false;
         }
         else
         {
            gvApikeys.Columns[0].Visible = false;
            gvApikeys.Columns[3].Visible = true;
         }
         gvApikeys.DataBind();
      }

      protected string formatExpired( object expired )
      {
         DateTime dt; DateTime.TryParse( expired.ToString(), out dt );
         if ( dt == DateTime.MinValue )
            return "&nbsp;";

         return dt.ToString();
      }

      protected void gvApikeys_RowDataBound( object sender, GridViewRowEventArgs e )
      {
         if ( e.Row.RowType == DataControlRowType.DataRow )
         {
            apikeysResults row = e.Row.DataItem as apikeysResults;
         }
         else if ( e.Row.RowType == DataControlRowType.Footer )
         {
            e.Row.Cells[0].Text = string.Format( "Count: {0}", hidCount.Value );
         }
      }

      protected void gvApikeys_RowCommand( object sender, GridViewCommandEventArgs e )
      {
         if ( e.CommandName == "SelectApikey" )
         {
            string newApikey = e.CommandArgument.ToString();
            TextBox key = Parent.FindControl( "tbApikey" ) as TextBox;
            if ( key != null )
            {
               key.Text = newApikey;
            }
         }
      }
   }
}